package net.akkz.dao.impl;

import java.util.ArrayList;

import net.akkz.dao.FultureDao;
import net.akkz.dbFactory.Session;
import net.akkz.exception.BuildClassError;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.Fulture;

public class FultureDaoImpl implements FultureDao
{

	@Override
	public void addFeature(Session session, Fulture fulture) throws SQLExcuteException
	{
		String sql = "insert into fulture(message,date,studentid,joined) values(?, ?, ?, ?)";

		Object[] values = new Object[4];
		values[0] = fulture.getMessage();
		values[1] = fulture.getDate();
		values[2] = fulture.getStudentid();
		values[3] = fulture.getJoined();

		session.excute(sql, values);
	}

	@Override
	public ArrayList<Fulture> findFulture(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulture";

		return session.find(sql, new Object[0], Fulture.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Fulture> findFultureOrderByJoined(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulture order by joined";

		return session.find(sql, new Object[0], Fulture.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Fulture> findFultureByStudentId(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulture where studentid = ?";

		Object[] values = new Object[1];
		values[0] = studentId;

		return session.find(sql, values, Fulture.class);
	}

	@Override
	public Fulture getFulture(Session session, int fultureId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from fulture where id = ?";

		return session.get(sql, fultureId, Fulture.class);
	}

	@Override
	public void deleteFulture(Session session, int fultureId) throws SQLExcuteException
	{
		String sql = "delete from fulture where id = ?";

		Object[] values = new Object[1];
		values[0] = fultureId;

		session.excute(sql, values);
	}

	@Override
	public void updateFultureJoined(Session session, Fulture fulture) throws SQLExcuteException
	{
		String sql = "update fulture set message=?, date=?, studentid=?, joined=? where id=?";
		
		Object[] values = new Object[5];
		values[0] = fulture.getMessage();
		values[1] = fulture.getDate();
		values[2] = fulture.getStudentid();
		values[3] = fulture.getJoined();
		values[4] = fulture.getId();
		
		session.excute(sql, values);
	}

}
